package me.chinaq.fastweb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import me.chinaq.fastweb.bean.User;
import me.chinaq.fastweb.service.UserService;
import me.chinaq.fastweb.web.ApiResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;

import java.util.List;

/**
 * Created by CodeGenerator on 2017/07/05.
 */
@Scope
@RestController
@RequestMapping("/user")
public class UserController {
	private static final Logger LOGGER = Logger.getLogger(UserController.class);

	@Autowired(required = false)
	private UserService userService;

	@PostMapping("/add")
	public ApiResult add(User user) {
		userService.save(user);
		return ApiResult.success();
	}

	@PostMapping("/delete")
	public ApiResult delete(Integer id) {
		userService.deleteById(id);
		return ApiResult.success();
	}

	@PostMapping("/update")
	public ApiResult update(User user) {
		userService.updateTest(user);
		return ApiResult.success();
	}

	@GetMapping("/detail")
	public ApiResult detail(Integer id) {
		User user = userService.findById(id);
		return ApiResult.success(user);
	}

	@PostMapping("/list")
	public ApiResult list(Integer page, Integer size) {
		PageHelper.startPage(page, size);
		List<User> list = userService.findAll();
		Condition condition = new Condition(User.class);
		condition.createCriteria().andCondition("nick_name like '%1%'");
		userService.findByCondition(condition);
		PageInfo pageInfo = new PageInfo(list);
		return ApiResult.success(pageInfo);
	}
}
